//
//  CJUtil.m
//  NtoccShipper
//
//  Created by zcj on 2018/4/4.
//  Copyright © 2018年 zhuchongjin All rights reserved.
//

#import "CJUtil.h"
#import <sys/utsname.h>

@implementation CJUtil : NSObject 

+(NSString *)replaceStringWithAsterisk:(NSString *)originalStr startLocation:(NSInteger)startLocation lenght:(NSInteger)lenght{
    
    NSString *newStr = originalStr;
    for (int i = 0; i < lenght; i++) {
        
        NSRange range = NSMakeRange(startLocation, 1);
        
        newStr = [newStr stringByReplacingCharactersInRange:range withString:@"*"];
        
        startLocation ++;
    }
    
    return newStr;
}
//+ (UILabel *)labelWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor  cornerRadius:(float)cornerRadius font:(UIFont *)font textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment text:(NSString *)text{
//    UILabel *view = [[UILabel alloc] initWithFrame:frame];
//    
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//    
//    if (cornerRadius > 0) {
//        view.layer.cornerRadius = cornerRadius;
//        view.layer.masksToBounds = YES;
//    }
//    
//    if (font) {
//        view.font = font;
//    }
//    
//    if (textColor) {
//        view.textColor = textColor;
//    }
//    
//    view.textAlignment = textAlignment;
//    
//    if (text) {
//        view.text = text;
//    }
//    
//    return view;
//}

//+ (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor backgroundColor:(UIColor *)backgroundColor titleFont:(UIFont *)titleFont cornerRadius:(float)cornerRadius Target:(id)target action:(SEL)action{
//    UIButton *view = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    view.frame = frame;
//    
//    if (title) {
//        [view setTitle:title forState:UIControlStateNormal];
//    }
//    
//    if (titleColor) {
//        [view setTitleColor:titleColor forState:UIControlStateNormal];
//    }
//    
//    
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//    
//    if (titleFont) {
//        view.titleLabel.font = titleFont;
//    }
//    
//    if (cornerRadius > 0) {
//        view.layer.cornerRadius = cornerRadius;
//        view.layer.masksToBounds = YES;
//    }
//    
//    if (target && action) {
//        [view addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//    }
//    
//    return view;
//}

//+ (UIView *)viewWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor  cornerRadius:(float)cornerRadius{
//    UIView *view = [[UIView alloc] initWithFrame:frame];
//
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//
//
//    if (cornerRadius > 0) {
//        view.layer.cornerRadius = cornerRadius;
//        view.layer.masksToBounds = YES;
//    }
//
//    return view;
//}

//+ (UIImageView *)imageViewWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor borderColor:(UIColor *)borderColor cornerRadius:(float)cornerRadius image:(UIImage *)image contentMode:(NSNumber *)contentMode{
//    UIImageView *view = [[UIImageView alloc] initWithFrame:frame];
//
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//
//    if (cornerRadius > 0) {
//        view.layer.cornerRadius = cornerRadius;
//        view.layer.masksToBounds = YES;
//    }
//
//    if (image) {
//        [view setImage:image];
//    }
//
//    if (contentMode.integerValue) {
//        view.contentMode = [contentMode integerValue];
//    }
//
//    return view;
//}


//+ (UITableView *)tableViewFrame:(CGRect)frame style:(UITableViewStyle)style backgroundColor:(UIColor *)backgroundColor separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle dataSource:(id<UITableViewDataSource>)dataSource delegate:(id<UITableViewDelegate>)delegate{
//    UITableView *view = [[UITableView alloc] initWithFrame:frame style:style];
//
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//
//    view.separatorStyle = separatorStyle;
//
//    if (dataSource) {
//        view.dataSource = dataSource;
//    }
//
//    if (delegate) {
//        view.delegate = delegate;
//    }
//
//    return view;
//}

//+ (UITextField *)textFieldWithFrame:(CGRect)frame placeholder:(NSString *)placeholder backgroundColor:(UIColor *)backgroundColor borderColor:(UIColor *)borderColor cornerRadius:(float)cornerRadius font:(UIFont *)font textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment text:(NSString *)text delegate:(id<UITextFieldDelegate>)delegate{
//    
//    UITextField *view = [[UITextField alloc] initWithFrame:frame];
//    
//    if (placeholder) {
//        view.placeholder = placeholder;
//    }
//    
//    if (backgroundColor) {
//        view.backgroundColor = backgroundColor;
//    }
//    
//    
//    
//    if (cornerRadius > 0) {
//        view.layer.cornerRadius = cornerRadius;
//        view.layer.masksToBounds = YES;
//    }
//    
//    if (font) {
//        view.font = font;
//    }
//    
//    if (textColor) {
//        view.textColor = textColor;
//    }
//    
//    if (text) {
//        view.text = text;
//    }
//    
//    if (delegate) {
//        view.delegate = delegate;
//    }
//    
//    view.textAlignment = textAlignment;
//    
//    return view;
//}

+ (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return [reSizeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = font;
    [label sizeToFit];
    return label.frame.size.width;
}
+ (void)getImageViewCutRound:(UIImageView *)imageV{
    UIGraphicsBeginImageContextWithOptions(imageV.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:imageV.bounds cornerRadius:imageV.frame.size.width] addClip];
    [imageV drawRect:imageV.bounds];
    imageV.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

+ (void)getUIButtonCutAllCornRound:(UIButton *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight |UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
}
+ (void)getUIBUttonCutTopLeftAndBottomLeft:(UIButton *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
    
}

+ (void)getUIButtonCutTopLeftAndBottomLeft:(UIButton *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
    
}
+ (void)getUIButtonCutTopRightAndBottomRight:(UIButton *)sender height:(CGFloat )height{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
    
}
+ (void)getUIImageVCutAllCornRound:(UIImageView *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight |UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
}

+ (void)getUILabelCutAllCornRound:(UILabel *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight |UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
}
+ (void)getUILabelCutTopLeftAndBottomLeft:(UILabel *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
    
}
+ (void)getUILabelCutTopRightAndBottomRight:(UILabel *)sender height:(CGFloat )height{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
    
}
+ (void)getViewCutAllCornRound:(UIView *)sender height:(CGFloat )height{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRight |UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(height, height)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = sender.bounds;
    maskLayer.path = maskPath.CGPath;
    sender.layer.mask = maskLayer;
}





+(void)getRightTextWithTextField:(UITextField *)textField textStr:(NSString *)str labelWidth:(CGFloat )width labbelHeight:(CGFloat )height{
    
    UILabel *rightView = [[UILabel alloc]init];
    rightView.text = str;
    rightView.textColor = [UIColor cjMainTextColor];
    rightView.font = [UIFont cjTitleFont14];
    rightView.size = CGSizeMake(width,height);
    rightView.contentMode = UIViewContentModeCenter;
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
}
+(void)getLeftTextWithTextField:(UITextField *)textField textStr:(NSString *)str labelWidth:(CGFloat )width labelHeight:(CGFloat )height{
    
    UILabel *rightView = [[UILabel alloc]init];
    rightView.text = str;
    rightView.textColor = [UIColor cjMainTextColor];
    rightView.font = [UIFont cjTitleFont14];
    rightView.size = CGSizeMake(width,height);
    rightView.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}


/**
 左侧 提示 课自设置大小

 @param textField textField description
 @param str <#str description#>
 @param height <#height description#>
 @param font <#font description#>
 */
+(void)getLeftTextWithTextField:(UITextField *)textField textStr:(NSString *)str  labelHeight:(CGFloat )height font:(UIFont *)font{
    
    UILabel *rightView = [[UILabel alloc]init];
    rightView.text = str;
    rightView.textColor = [UIColor cjMainTextColor];
    rightView.font = font;
    rightView.size = CGSizeMake([CJUtil getWidthWithTitle:str font:font],height);
    rightView.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}


+(void)getLeftAndRightTextWithTextField:(UITextField *)textField textLeftStr:(NSString *)str labelLW:(CGFloat )Lwidth labelLH:(CGFloat )Lheight textLeftStr:(NSString *)str1 labelRW:(CGFloat )Rwidth labelRH:(CGFloat )Rheight{
    
    UILabel *rightView = [[UILabel alloc]init];
    rightView.text = str1;
    rightView.textColor = [UIColor cjMainTextColor];
    rightView.font = [UIFont cjTitleFont14];

    rightView.size = CGSizeMake(Rwidth,Rheight);
    rightView.contentMode = UIViewContentModeCenter;
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *rightView1 = [[UILabel alloc]init];
    rightView1.text = str;
    rightView1.textColor = [UIColor cjMainTextColor];
    rightView1.size = CGSizeMake(Lwidth,Lheight);
    rightView1.font = [UIFont cjTitleFont14];
    rightView1.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView1;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
}


// 实现图文混排的方法
+ (NSAttributedString *) creatAttrStringWithText:(NSString *) text image:(UIImage *) image{
    
    // NSTextAttachment可以将图片转换为富文本内容
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    // 通过NSTextAttachment创建富文本
    // 图片的富文本
    NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:attachment];
    NSMutableAttributedString *mutableImageAttr = [[NSMutableAttributedString alloc] initWithAttributedString:imageAttr];
    [mutableImageAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, imageAttr.length)];
    // 调整图片的位置，负数代表向
    [mutableImageAttr addAttribute:NSBaselineOffsetAttributeName value:@(-5) range:NSMakeRange(0, imageAttr.length)];
    
    // 文字的富文本
    NSAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont cjTitleFont14]}];
    
    NSMutableAttributedString *mutableAttr = [[NSMutableAttributedString alloc] init];
    
    // 将图片、文字拼接
    // 如果要求图片在文字的后面只需要交换下面两句的顺序
    [mutableAttr appendAttributedString:textAttr];
    [mutableAttr appendAttributedString:mutableImageAttr];
    return [mutableAttr copy];
}

//+ (NSAttributedString *) creatAttrStringWithText:(NSString *) text strWI:(NSString *)text1{
//    
//    
//}

@end
